<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Admin\Controller;

use Think\AjaxPage;

/**
 * 分销应用
 */
class AppDistributController extends BaseController {

    /**
     * 分销关系
     */
    public function index() {
        $user_id = I('post.user_id');
        $level = D('UserLevel')->getField('level_id,level_name');
        if (!empty($user_id)) {
            $rs = D('users')->where(array('is_distribut' => 1, 'first_leader' => array('eq', 0), 'user_id' => $user_id))->field('user_id,nickname,level')->select();
        } else {
            $rs = D('users')->where(array('is_distribut' => 1, 'first_leader' => array('eq', 0)))->field('user_id,nickname,level')->order('user_id desc')->select();
        }
        foreach ($rs as $k => $v) {
            $rs[$k]['levelName'] = $level[$v['level']];  //蓝钻
            $rs1 = D('users')->where(array('first_leader' => $v['user_id']))->field('user_id,nickname,level')->select();
            if (count($rs1)) {
                foreach ($rs1 as $k1 => $v1) {
                    $rs1[$k1]['levelName'] = $level[$v1['level']];
                    //黄金
                    $rs2 = D('users')->where(array('first_leader' => $v1['user_id']))->field('user_id,nickname,level')->select();
                    if (count($rs2)) {
                        foreach ($rs2 as $k2 => $v2) {
                            $rs2[$k2]['levelName'] = $level[$v2['level']];
                            $rs3 = D('users')->where(array('first_leader' => $v2['user_id']))->field('user_id,nickname,level')->select();
                            if (count($rs3)) {
                                foreach ($rs3 as $k3 => $v3) {
                                    $rs3[$k2]['levelName'] = $level[$v3['level']];
                                }
                                $rs2[$k2]['first'] = $rs3;
                            }
                        }
                        $rs1[$k1]['first'] = $rs2;
                    }
                }
                $rs[$k]['first'] = $rs1;
            }
        }

//        foreach ($rs as $k => $v) {
//            $rs1 = D('users')->where(array('first_leader' => $v['user_id']))->getField('user_id,user_id,nickname,level');
//            if (count($rs1)) {
//                $rs[$k]['first'] = $rs1;
//                foreach ($rs1 as $k1 => $v1) {
//                    $rs2 = D('users')->where(array('first_leader' => $k1))->getField('user_id,user_id,nickname,level');
//                    if (count($rs2)) {
//                        $rs[$k]['first'][$k1]['first'] = $rs2;
////                        foreach ($rs2 as $k2 => $v2) {
////                            $rs3 = D('users')->where(array('first_leader' => $k2))->getField('user_id,user_id,nickname');
////                            $rs[$k]['first'][$k1]['first'][$k2]['first'] = $rs3;
////                        }
//                    }
//                }
//            }
//        }
        $this->assign('distributRs', $rs);
        $this->assign('user_id', $user_id);
        $this->display();
    }

    /**
     * 分销设置
     */
    public function set() {
        $inc_type = 'distribut';
        $this->assign('inc_type', $inc_type);
        $this->assign('config', xrkCache($inc_type));
        if (IS_POST) {
            $param = I('post.');
            $inc_type = $param['inc_type'];
            unset($param['inc_type']);
            xrkCache($inc_type, $param);
            $this->success("操作成功");
            exit();
        }

        if (!file_exists("Public/upload/qrcode/qrcode_level_5.png")) {
            Vendor('phpqrcode.phpqrcode');
            $level = 1; //容错级别
            $size = 6; //生成图片大小
//            $url =  "http://b2c.xiarikui.cn/index.php/Mobile/Loginbind/register/level/5.html"; // U('Mobile/AppDistribut/followDistribut', array('user_id', $userId));
            $url = SITE_URL . "/index.php/Mobile/Loginbind/register/level/5.html";
            //保存的路径+图片名
            $QRname = "Public/upload/qrcode/qrcode_level_5.png";
            \QRcode::png($url, $QRname, $level, $size, 2);
            $logo = SITE_URL . '/Public/upload/qrcode/logo.png';
            if ($logo !== FALSE) {
                $QR = imagecreatefromstring(file_get_contents($QRname));
                $logo = imagecreatefromstring(file_get_contents($logo));
                $QR_width = imagesx($QR); //二维码图片宽度
                $QR_height = imagesy($QR); //二维码图片高度
                $logo_width = imagesx($logo); //logo图片宽度
                $logo_height = imagesy($logo); //logo图片高度
                $logo_qr_width = $QR_width / 4;
                $scale = $logo_width / $logo_qr_width;
                $logo_qr_height = $logo_height / $scale;
                $from_width = ($QR_width - $logo_qr_width) / 2;
                //重新组合图片并调整大小
                imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
            }
            unlink($QRname);
            //输出图片
            imagepng($QR, $QRname);
        }
        $this->display();
    }

    /**
     * 申请列表
     */
    public function apply() {
        $this->display();
    }

    public function ajaxApply() {
        $condition = array();
        if (IS_POST) {
            if (I('status') != 0) {
                $condition['status'] = I('status');
            }
            if (I('user_id')) {
                $condition['user_id'] = I('user_id');
            }
            if (I('apply_id')) {
                $condition['apply_id'] = I('apply_id');
            }
        }

        $model = D('AppDistributApply');
        $count = $model->where($condition)->count();
        $Page = new AjaxPage($count, 10);
        foreach ($condition as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $list = $model->where($condition)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        foreach ($list as $key => $value) {
            $user = D('users')->where(array('user_id' => $value['user_id']))->field('nickname')->find();
            $list[$key]['nickname'] = $user['nickname'];
            $user1 = D('users')->where(array('user_id' => $value['apply_id']))->field('nickname')->find();
            $list[$key]['apply_nickname'] = $user1['nickname'];
        }
        $show = $Page->show();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }

    /**
     * 删除申请
     */
    public function delApply() {
        $id = I('id');
        if (empty($id)) {
            $this->error('参数错误!');
        }
        D('AppDistributApply')->where(array('id' => $id))->delete();
        $this->success('成功删除!');
    }

    /**
     * 审核申请 
     */
    public function auditApply() {
        $id = I('id');
        if (empty($id)) {
            $this->error('参数错误!');
        }

        $apply = D('AppDistributApply')->where(array('id' => $id))->find();
//        $user = D('users')->where(array('user_id' => $apply['apply_id']))->field('user_id,user_money,nickname,is_distribut')->find();
        if (IS_POST) {
            $data['status'] = I('post.status');
            D('AppDistributApply')->where(array('id' => $id))->save($data);
            if (I('post.status') == 2) {
                D('users')->where(array('user_id' => $apply['apply_id']))->setField('is_distribut', 1);
                D('AppDistributApply')->where(array('id' => $id))->setField('status', 2);
            }
            $this->success('操作成功!', U('Admin/AppDistribut/apply'));
            exit();
        }
        $user = D('users')->where(array('user_id' => $apply['user_id']))->field('nickname')->find();
        $apply['nickname'] = $user['nickname'];
        $user1 = D('users')->where(array('user_id' => $apply['apply_id']))->field('nickname')->find();
        $apply['apply_nickname'] = $user1['nickname'];

        $this->assign('apply', $apply);
        $this->assign('user', $user);
        $this->display();
    }

    /**
     * 提现列表
     */
    public function cash() {
        $this->display();
    }

    public function ajaxCash() {
        $condition = array();
        $type = I('type', 1);
        $condition['type'] = $type;
        if (IS_POST) {
            if (I('status') != '-1') {
                $condition['status'] = I('status');
            }
            if (I('user_id')) {
                $condition['user_id'] = I('user_id');
            }
            if (I('account_bank')) {
                $condition['account_bank'] = I('account_bank');
            }
            if (I('account_name')) {
                $condition['account_name'] = I('account_name');
            }
        }

        $model = D('cash');
        $count = $model->where($condition)->count();
        $Page = new AjaxPage($count, 10);
        foreach ($condition as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $cashList = $model->where($condition)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        $show = $Page->show();
        $this->assign('cashList', $cashList);
        $this->assign('page', $show);
        $this->display();
    }

    /**
     * 删除提现记录
     */
    public function delCash() {
        $id = I('id');
        if (empty($id)) {
            $this->error('参数错误!');
        }

        D('cash')->where(array('id' => $id))->delete();
        $this->success('成功删除!');
    }

    /**
     * 审核提现
     */
    public function auditCash() {
        $id = I('id');
        if (empty($id)) {
            $this->error('参数错误!');
        }

        $cash = D('cash')->where(array('id' => $id))->find();
        $user = D('users')->where(array('user_id' => $cash['user_id']))->field('user_id,user_money,nickname')->select();
        if (IS_POST) {
            $data['status'] = I('post.status');
            $data['remark'] = I('post.remark');
            D('cash')->where(array('id' => $id))->save($data);
            ////
            if (I('post.status') == 1) {
                $user_money = floatval($user[0]['user_money']) - floatval($cash['money']);
                D('users')->where(array('user_id' => $cash['user_id']))->setField('user_money', $user_money);
                ////
                $user_log_data['user_id'] = $cash['user_id'];
                $user_log_data['user_money'] = -$cash['money'];
                $user_log_data['change_time'] = time();
                $user_log_data['desc'] = '平台提现';
                D('user_log')->add($user_log_data);
            }
            $this->success('操作成功!', U('Admin/AppDistribut/cash'));
            exit();
        }

        $this->assign('cash', $cash);
        $this->assign('user', $user[0]);
        $this->display();
    }

    /**
     * 汇款列表
     */
    public function record() {
        $this->display();
    }

    public function ajaxRecord() {
        $condition = array();
        if (IS_POST) {
            if (I('user_id')) {
                $condition['user_id'] = I('user_id');
            }
            if (I('account_bank')) {
                $condition['account_bank'] = I('account_bank');
            }
            if (I('account_name')) {
                $condition['account_name'] = I('account_name');
            }
        }
        $condition['status'] = 1;
        $model = D('cash');
        $count = $model->where($condition)->count();
        $Page = new AjaxPage($count, 10);
        foreach ($condition as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $cashList = $model->where($condition)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        $show = $Page->show();
        $this->assign('cashList', $cashList);
        $this->assign('page', $show);
        $this->display();
    }

    /**
     * 分成列表
     */
    public function bonus() {
        $this->display();
    }

    public function ajaxBonus() {
        $condition = array();
        if (IS_POST) {
            if (I('status') != '-1') {
                $condition['status'] = I('status');
            }
            if (I('user_id')) {
                $condition['user_id'] = I('user_id');
            }
            if (I('order_sn')) {
                $condition['order_sn'] = I('order_sn');
            }
        }
        $model = D('bonus_log');
        $count = $model->where($condition)->count();
        $Page = new AjaxPage($count, 10);
        foreach ($condition as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $bonusList = $model->where($condition)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        $show = $Page->show();
        $this->assign('bonusList', $bonusList);
        $this->assign('page', $show);
        $this->display();
    }

    /**
     * 修改分成备注
     */
    public function editBonus() {
        $id = I('id');
        if (empty($id)) {
            $this->error('参数错误!');
        }

        $bonus = D('bonus_log')->where(array('id' => $id))->find();
        if (IS_POST) {
            $data['remark'] = I('post.remark');
            D('bonus_log')->where(array('id' => $id))->save($data);
            $this->success('操作成功!', U('Admin/AppDistribut/bonus'));
            exit();
        }

        $this->assign('bonusLog', $bonus);
        $this->display();
    }

}
